<?php

/**
 * 充值申请 
 */
class Application_Model_RechargeBill extends Application_Model_DomainObject implements Application_Model_PayInterface {
    
    protected $_id;
    
    protected $_name;
    
    protected $_dateCreated;
    
    protected $_money;
    
    protected $_status;
    
    protected $_userId;
    
    protected $_datePaid;
    /**
     * 活动开始时间
     */
    const RechargeActivityStart = "2014-06-01";
    /**
     * 活动结束时间
     */
    const RechargeActivityEnd = "2014-06-05";

    public function canPay($userId) {
        if($this->_status == Application_Model_PayBill::PAY_STATUS_SUCCESS)
            return false;
        if($this->_userId!= $userId)
            return FALSE;
        return TRUE;            
    }


    public function getMoney(){
        return $this->_money;
    }
    
    public function getPayName() {
        return '悠奇-充值';
    }
    
    public function getPayCommen() {
        return "充值流水号：".$this->_id;
    }
    
    public function isRepeatPay() {
        return true;
    }

    public function setPaid() {
        $this->_status = Application_Model_PayBill::PAY_STATUS_SUCCESS;   
        $this->_datePaid = date('Y-m-d H:i:s');
        $generalizeItem = Application_Model_Generalize::getItemByParticipate($this->_id,  Application_Model_GeneralizeRule::Grant_Recharge);
        if($generalizeItem && !$generalizeItem->getJudgeIfEffective()) {
            $generalizeItem->receive($this->_userId, Application_Model_GeneralizeRule::Grant_Recharge, $this->_id);
        }
    }
   
    public static function getCountByUserId($userId)
    {
        return self::getDao()->count(array("userId = "=> $userId));
    }

    public function getBillNo() {
        return $this->_id;
    }
    
    public function getPayDescription() {
        return '充值到悠奇帐户，用户名：'.Application_Model_User::load($this->_userId)->Email;
    }
    
    /**
     * 跟据用户返回他的充值记录
     * @param type $userId
     * @param type $status
     * @param string $dateStart
     * @param string $dateEnd
     * @return type
     */
    public static function getByUserId($userId,$status,$dateStart=null,$dateEnd=null){
        if($dateStart == NULL){
            $dateStart = '2000-01-01 12:00';
        }
        if($dateEnd == NULL){
            $dateEnd = '2020-01-01 12:00';
        }
        return self::getDao()->queryAll(array('userId'=>$userId, 'status'=>$status ,' and datePaid >= ' =>$dateStart, ' and datePaid <= ' => $dateEnd));
    }
    
    public static function countRecharge($userId,$status,$dateStart,$dateEnd) {
        $where = array();
        $and = "";
        if($userId != "") {
            $where[" userId = "] = $userId;
            $and = " and ";
        }
        if($status != "") {
            $where[$and." status = "] = $status;
            $and = " and ";
        }
        if($dateStart != "") {
            $where[$and." datePaid > "] = $dateStart;
            $and = " and ";
        }
        if($dateEnd != "") {
            $where[$and." datePaid <= "] = $dateStart;
            $and = " and ";
        }
        return self::getDao()->count($where);
    }
    
    public function getUserInfo() {
        return Application_Model_User::load($this->_userId);
    }
}